import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './router'
import Cookie from 'js-cookie'
// import store from '@/store/index'
// 解决vuerouter重复点击报错
const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, onComplete = () => {}, onAbort) {
  return originPush.call(this, location, onComplete, onAbort)
};

Vue.use(VueRouter)

const router = new VueRouter({
  routes
})
// 
// 路由拦截
router.beforeEach((to, from, next) => {
  const loginStatus = Cookie.get('userInfo')
  if (to.name === 'login') {
    next()
  } else if (to.name === '404') {
    next()
  } else {
    if (loginStatus != undefined && loginStatus != null) {
      // if (!store.state.userMsg.id) {
      //  store.dispatch('getUserInfo')
      // }
      next()
    } else {
      next('/login')
    }
  }
})
router.afterEach((to, from) => {
  setTimeout(() => {
    sessionStorage.setItem('br', from.path)
  }, 1000)
})
export default router